package com.aletto.general_store.ui.activities;

import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

import com.aletto.general_store.R;
import com.aletto.general_store.data.entities.WalletAccount;
import com.aletto.general_store.ui.popup.ApprovePopup;
import com.aletto.general_store.data.ERC20ServiceAbi;
import com.aletto.general_store.utils.LocalConfigStore;
import com.aletto.general_store.utils.XHelper;
import com.lxj.xpopup.XPopup;
import com.sisyphus.spinach.network.utils.RxUtil;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class ApproveActivity extends AppCompatActivity {

    private TextView approveAmount;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_approve);

        XHelper.initTitle(this, R.string.approval);

        approveAmount = findViewById(R.id.tv_approve);
        TextView walletAddress = findViewById(R.id.tv_wallet_address);

        findViewById(R.id.iv_approval).setOnClickListener(v -> allowance());

        findViewById(R.id.layout_1).setOnClickListener(v ->
                new XPopup.Builder(v.getContext())
                        .asCustom(new ApprovePopup(v.getContext(), s -> approveAmount.setText(s)))
                        .show()
        );

        WalletAccount wallet = LocalConfigStore.getInstance().getWallet();
        walletAddress.setText(wallet.getAddress());
        approveAmount.setText("0.00 USDT");

        allowance();
    }

    private void allowance() {
        WalletAccount wallet = LocalConfigStore.getInstance().getWallet();

        Observable.create((ObservableOnSubscribe<BigDecimal>) emitter -> {

                    BigDecimal allowance = ERC20ServiceAbi.allowance(
                            ERC20ServiceAbi.USDT_CONTRACT_ADDRESS,
                            wallet.getAddress(),
                            ERC20ServiceAbi.MARKET_CONTRACT_ADDRESS
                    );

                    emitter.onNext(allowance);
                    emitter.onComplete();
                })
                .compose(RxUtil.transformer())
                .subscribe(new Observer<>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull BigDecimal bigDecimal) {
                        String format = new DecimalFormat("0.00").format(bigDecimal);
                        approveAmount.setText(format + " USDT");
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
